package com.emode.util;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtil
{
  private static final String patternDateShort = "yyyyMMdd";
  private static final String patternDate = "yyyy-MM-dd";
  private static final String patternDateTime = "yyyy-MM-dd HH:mm:ss";
  private static final String patternDateMinutes = "yyyy-MM-dd HH:mm";
  public static final long ONE_MINUTE = 60000L;
  public static final long ONE_HOUR = 3600000L;
  public static final long ONE_DAY = 86400000L;
  public static final long ONE_WEEK = 604800000L;
  public static final SimpleDateFormat MIDDLE_PATTEN = new SimpleDateFormat("MMM dd, yyyy 'at' HH:mm a", Locale.US);

  public static final SimpleDateFormat SMALL_PATTEN = new SimpleDateFormat("MMM dd, yyyy", Locale.US);

  public static String getDatePattern() {
    return "yyyy-MM-dd";
  }

  public static String getDateTimePattern() {
    return getDatePattern() + " HH:mm:ss";
  }

  public static String getLastDate(int year, int month) {
    int[] monthDay = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
      monthDay[1] = 29;
    }
    int monthDayNum = monthDay[(month - 1)];
    String end = year + "-" + month + "-" + monthDayNum;
    return end;
  }

  public static String getShortDate(Date myDate)
  {
    SimpleDateFormat fd = new SimpleDateFormat("yyyyMMdd");
    return fd.format(myDate);
  }

  public static String formatDateTime(Date myDate, String pattern)
  {
    myDate = isDate(myDate);

    SimpleDateFormat fd = new SimpleDateFormat(pattern);
    return fd.format(myDate);
  }

  public static String formatDateTime(String myDate, String pattern)
  {
    if (myDate.length() > 10)
      myDate = myDate.substring(0, 10);
    Date date = getDateByString(myDate, "yyyy-MM-dd");
    return formatDateTime(date, pattern, Locale.US);
  }

  public static String formatDateTime(Date myDate, String pattern, Locale localcode)
  {
    myDate = isDate(myDate);
    SimpleDateFormat fd = new SimpleDateFormat(pattern, localcode);
    return fd.format(myDate);
  }

  public static Date isDate(Date myDate)
  {
    return myDate;
  }

  public static long getQuot(Date time1, Date time2)
  {
    long quot = 0L;
    try {
      quot = time1.getTime() - time2.getTime();
      quot = quot / 1000L / 60L / 60L / 24L;
    }
    catch (Exception e) {
    }
    return quot;
  }

  public static Integer getTodayTime() {
    Calendar today = Calendar.getInstance();
    today.set(11, 0);
    today.set(12, 0);
    today.set(13, 0);
    return Integer.valueOf(String.valueOf(today.getTimeInMillis()).substring(0, 10));
  }

  public static Integer getTomorrowTime() {
    Calendar tomorrow = Calendar.getInstance();
    tomorrow.set(11, 24);
    tomorrow.set(12, 0);
    tomorrow.set(13, 0);
    return Integer.valueOf(String.valueOf(tomorrow.getTimeInMillis()).substring(0, 10));
  }

  public static String getTipDay(String date) {
    long quot = getQuot(parse(date), new Date());
    if (quot == 0L)
      return "今天";
    if (quot == 1L)
      return "明天";
    if (quot == 2L)
      return "后天";
    return "";
  }

  public static int dayOfWeek(Object date1)
  {
    Date date = (Date)date1;

    Calendar aCalendar = Calendar.getInstance();

    aCalendar.setTime(date);

    int x = aCalendar.get(7);
    return x;
  }

  public static String dayOfWeek2(Object date1) {
    Date date = (Date)date1;

    Calendar aCalendar = Calendar.getInstance();

    aCalendar.setTime(date);

    int x = aCalendar.get(7);
    return dayOfWeekByDayNum(x);
  }

  public static String dayOfWeekByDayNum(int x) {
    String str = "周日";
    if (x == 7)
      str = "周六";
    else if (x == 6)
      str = "周五";
    else if (x == 5)
      str = "周四";
    else if (x == 4)
      str = "周三";
    else if (x == 3)
      str = "周二";
    else if (x == 2) {
      str = "周一";
    }
    return str;
  }

  public static Date getDateOfCurrentWeek(int day)
  {
    Calendar aCalendar = Calendar.getInstance();
    int x = aCalendar.get(7);
    aCalendar.add(7, day - (x - 1));
    return aCalendar.getTime();
  }

  public static int getWeekOfMonth(Date date)
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(4);
  }

  public static Date addSomeDay(Date oldDate, int num) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(oldDate);
    calendar.add(5, num);
    return calendar.getTime();
  }

  public static Date addHour(Date oldDate, int num) {
    oldDate.setTime(oldDate.getTime() + num * 3600 * 1000);
    return oldDate;
  }

  public static String Dateformat(String date)
  {
    return date.replaceAll("-", "");
  }

  public static String getWeekDay(String date)
  {
    return formatDateTime(parse(date), "EE");
  }

  public static String formatDate(Date myDate)
  {
    return formatDateTime(myDate, "yyyy-MM-dd");
  }

  public static String formatDateMinutes(Date myDate)
  {
    return formatDateTime(myDate, "yyyy-MM-dd HH:mm");
  }

  public static String formatDateMinutes(Date myDate, String pattern)
  {
    return formatDateTime(myDate, pattern);
  }

  public static String formatDateTime(Date myDate)
  {
    return formatDateTime(myDate, "yyyy-MM-dd HH:mm:ss");
  }

  public static Date getDateByString(String rq)
  {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date d = null;
    try {
      d = df.parse(rq);
    }
    catch (Exception e) {
    }
    return d;
  }

  public static Date getDateString(String rq)
  {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    Date d = null;
    try {
      d = df.parse(rq);
    }
    catch (Exception e) {
    }
    return d;
  }

  public static Date parse(String param)
  {
    Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try {
      date = sdf.parse(param);
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return date;
  }

  public static Date getCurrentMonthFirstDay()
  {
    Calendar calendar = Calendar.getInstance();
    Date date = calendar.getTime();
    date = getDateByString(formatDate(date));
    calendar.setTime(date);
    calendar.set(5, 1);
    return calendar.getTime();
  }

  public static Integer getIntCurrentMonthFirstDay()
  {
    Calendar calendar = Calendar.getInstance();
    Date date = calendar.getTime();
    date = getDateByString(formatDate(date));
    calendar.setTime(date);
    calendar.set(5, 1);
    return Integer.valueOf(convertDateToInt(calendar.getTime()));
  }

  public static Integer getIntPreMonthFirstDay(Integer last_month)
  {
    Date date = convertInt2Date(last_month.intValue());
    GregorianCalendar aGregorianCalendar = new GregorianCalendar();
    aGregorianCalendar.setTime(date);
    aGregorianCalendar.set(2, aGregorianCalendar.get(2) - 1);
    aGregorianCalendar.set(5, 1);
    return Integer.valueOf(convertDateToInt(aGregorianCalendar.getTime()));
  }

  public static Integer getIntNextMonthFirstDay(Integer last_month)
  {
    Date date = convertInt2Date(last_month.intValue());
    GregorianCalendar aGregorianCalendar = new GregorianCalendar();
    aGregorianCalendar.setTime(date);
    aGregorianCalendar.set(2, aGregorianCalendar.get(2) + 1);
    aGregorianCalendar.set(5, 1);
    return Integer.valueOf(convertDateToInt(aGregorianCalendar.getTime()));
  }

  public static Date addMonth(Date date, int num)
  {
    Calendar startDT = Calendar.getInstance();
    startDT.setTime(date);
    startDT.add(2, num);
    return startDT.getTime();
  }

  public static Date addDay(Date date, int num)
  {
    Calendar startDT = Calendar.getInstance();
    startDT.setTime(date);
    startDT.add(5, num);
    return startDT.getTime();
  }

  public static Date addYear(Date date, int num)
  {
    Calendar startDT = Calendar.getInstance();
    startDT.setTime(date);
    startDT.add(1, num);
    return startDT.getTime();
  }

  public static Integer addDay(Integer time, int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(Long.valueOf(time + "000").longValue());
    calendar.add(11, day * 24);
    return Integer.valueOf(String.valueOf(calendar.getTimeInMillis()).substring(0, 10));
  }

  public static Date getDateTimeByString(String rq)
  {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Date d = null;
    try {
      d = df.parse(rq);
    }
    catch (Exception e) {
    }
    return d;
  }

  public static boolean isSameDay(Date c1, Date c2) {
    return formatDate(c1).equals(formatDate(c2));
  }

  public static Calendar string2Cal(String arg) {
    SimpleDateFormat sdf = null;
    String trimString = arg.trim();
    if (trimString.length() > 14)
      sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    else
      sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date d = null;
    try {
      d = sdf.parse(trimString);
    } catch (ParseException e) {
      return null;
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    return cal;
  }

  public static Date getCurrentDate()
  {
    Date currentTime = new Date();
    return currentTime;
  }

  public static String getCurrentStringToday() {
    String crrentdate = "";
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    Date currentTime = new Date();
    crrentdate = formatter.format(currentTime);
    return crrentdate;
  }

  public static String getStrWithArea(Date date)
  {
    DateFormat format = DateFormat.getDateInstance(2);
    return format.format(date);
  }

  public static String getGMTTimeZone(String timeZone)
  {
    String gmt = "";
    if ((timeZone != null) && !(StringUtils.isBlank(timeZone))) {
      int length = timeZone.length();
      String f = "";
      if (length == 1) {
        gmt = "GMT";
      } else if (timeZone.startsWith("-"))
      {
        f = timeZone.substring(1, length);
        gmt = "GMT+" + (Integer.parseInt(f) / 60);
      } else {
        gmt = "GMT-" + (Integer.parseInt(timeZone) / 60);
      }
    }
    return gmt;
  }

  public static String changeTimeZoneDate(Date value, String timeZone, SimpleDateFormat format)
  {
    if (value == null) {
      return null;
    }
    if (StringUtils.isBlank(timeZone))
      format.setTimeZone(TimeZone.getDefault());
    else {
      format.setTimeZone(TimeZone.getTimeZone(getGMTTimeZone(timeZone)));
    }
    return format.format(value);
  }

  public static String changeMillSecond2Date(long millSeconds)
  {
    Date temp = new Date(millSeconds);
    return formatDateMinutes(temp);
  }

  public static String changeMillSecond2Date(long millSeconds, String pattern) {
    Date temp = new Date(millSeconds);
    return formatDateMinutes(temp, pattern);
  }

  public static String dateFormat(String str) {
    String resultDate = null;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = null;
    try {
      date = simpleDateFormat.parse(str);
    }
    catch (ParseException e) {
      e.printStackTrace();
    }

    String cutDate = String.valueOf(System.currentTimeMillis() - date.getTime());
    String tmpDate = cutDate.substring(0, cutDate.length() - 3);
    int intDate = Integer.parseInt(tmpDate);
    if (intDate < 60) {
      resultDate = tmpDate + "秒前";
    }
    if (intDate == 60) {
      resultDate = "1分钟前";
    }
    if ((intDate > 60) && (intDate < 3600))
    {
      resultDate = String.valueOf(intDate / 60) + "分钟前";
    }
    if ((intDate >= 3600) && (intDate < 86400))
    {
      resultDate = String.valueOf(intDate / 3600) + "小时前";
    }

    if ((intDate > 86400) && (intDate < 864000))
    {
      resultDate = String.valueOf(intDate / 86400) + "天前";
    }

    if (intDate >= 864000) {
      SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
      resultDate = simpleDateFormat2.format(date);
    }

    return resultDate;
  }

  public static int getCurrentTime() {
    return Integer.valueOf(String.valueOf(Calendar.getInstance().getTimeInMillis()).substring(0, 10)).intValue();
  }

  public static int convertStringDateToInt(String str, String pattern) {
    return convertDateToInt(convertStringToDate(str, pattern));
  }

  public static int convertDateToInt(Date d) {
    long time = d.getTime() / 1000L;
    return (int)time;
  }

  public static Date convertStringToDate(String str, String pattern) {
    SimpleDateFormat sdf = null;
    try {
      sdf = new SimpleDateFormat(pattern);
    } catch (Exception ex) {
      sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }
    try {
      return sdf.parse(str);
    } catch (Exception e) {
    }
    return null;
  }

  public static String convertIntToDate(int date) {
    SimpleDateFormat sdf = new SimpleDateFormat("M-d HH:mm");

    Long x = Long.valueOf(Long.valueOf(date).longValue() * 1000L);
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(x.longValue());
    return sdf.format(c.getTime());
  }

  public static Date convertInt2Date(int date) {
    Long x = Long.valueOf(Long.valueOf(date).longValue() * 1000L);
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(x.longValue());
    return c.getTime();
  }

  public static String convertIntToDatePattern(int date, String pattern) {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    Long x = Long.valueOf(Long.valueOf(date).longValue() * 1000L);
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(x.longValue());
    return sdf.format(c.getTime());
  }

  public static String convertIntToDatePattern(int date) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日  HH:mm");
    Long x = Long.valueOf(Long.valueOf(date).longValue() * 1000L);
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(x.longValue());
    return dateFormat.format(c.getTime());
  }

  public static String convertIntToDatePattern2(int date) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Long x = Long.valueOf(Long.valueOf(date).longValue() * 1000L);
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(x.longValue());
    return dateFormat.format(c.getTime());
  }

  public static String convertIntToDate(int date, String language) {
    SimpleDateFormat sdf = new SimpleDateFormat(language);
    Long x = Long.valueOf(Long.valueOf(date).longValue() * 1000L);
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(x.longValue());
    return sdf.format(c.getTime());
  }

  public static String convertToString(Object o) {
    if (o instanceof Date) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      return sdf.format((Date)o);
    }
    return "";
  }

  public static Date getDateByString(String str, String pattern) {
    SimpleDateFormat sdf = null;
    try {
      sdf = new SimpleDateFormat(pattern);
    } catch (Exception ex) {
      sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }
    try {
      return sdf.parse(str);
    } catch (Exception e) {
    }
    return null;
  }

  public static String getPubDate(Integer time, String language) {
    Date date2;
    String timeStr = String.valueOf(time);
    Long date = Long.valueOf(Long.parseLong(timeStr + "000"));
    Calendar today = Calendar.getInstance();
    today.set(11, 0);
    today.set(12, 0);
    today.set(13, 0);
    Calendar yesterday = Calendar.getInstance();
    yesterday.set(11, -24);
    yesterday.set(12, 0);
    yesterday.set(13, 0);
    Calendar yesterdayBef = Calendar.getInstance();
    yesterdayBef.set(11, -48);
    yesterdayBef.set(12, 0);
    yesterdayBef.set(13, 0);
    String reString = null;
    if (date.longValue() < yesterdayBef.getTimeInMillis()) {
      date2 = new Date(date.longValue());

      reString = formatDateTime(date2, "M月d日 HH:mm");
    } else if (date.longValue() < yesterday.getTimeInMillis()) {
      date2 = new Date(date.longValue());

      reString = "前天  " + formatDateTime(date2, "HH:mm:ss");
    } else if (date.longValue() < today.getTimeInMillis()) {
      date2 = new Date(date.longValue());

      reString = "昨天  " + formatDateTime(date2, "HH:mm:ss");
    } else {
      date2 = new Date(date.longValue());

      reString = "今天  " + formatDateTime(date2, "HH:mm:ss");
    }

    return reString;
  }

  public static Date getCurrentDay()
  {
    Calendar cal = Calendar.getInstance();

    cal.set(11, 0);
    cal.set(12, 0);
    cal.set(13, 0);
    cal.set(14, 0);

    return cal.getTime();
  }

  public static Date getCurrentHour()
  {
    Calendar cal = Calendar.getInstance();

    cal.set(12, 0);
    cal.set(13, 0);
    cal.set(14, 0);

    return cal.getTime();
  }

  public static Date getCurrentMonth()
  {
    Calendar cal = Calendar.getInstance();

    cal.set(5, 1);
    cal.set(11, 0);
    cal.set(12, 0);
    cal.set(13, 0);
    cal.set(14, 0);

    return cal.getTime();
  }

  public static Date getYesterday()
  {
    Calendar cal = Calendar.getInstance();

    cal.set(11, 0);
    cal.set(12, 0);
    cal.set(13, 0);
    cal.set(14, 0);
    cal.add(5, -1);

    return cal.getTime();
  }
}
